package com.yld.car.market;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.TextView;

import com.yld.car.common.NetworkProgressUtils;

public class SelectorCarStateActivity extends BaseActivity implements
		OnItemClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.base_list);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);

		this.settingTitle(R.id.title, "选择车源状态");

		list = (ListView) findViewById(R.id.list1);
		list.setOnItemClickListener(this);

		Intent intent = this.getIntent();
		updateState = intent.getBooleanExtra("updateState", false);
		//
		// cId = intent.getStringExtra("cId");
		// System.out.println("cId=====" + cId);
		utils = NetworkProgressUtils.getInstance();
		showBackButton();
		// showCancelListener("取消选择", cancelClickListener);
		initViews();
	}

	private boolean updateState;
	private NetworkProgressUtils utils;
	private ListView list;

	// private OnClickListener cancelClickListener = new OnClickListener() {
	//
	// @Override
	// public void onClick(View v) {
	// mApp.setmProvinceInfo(null);
	// SharedPreferences sh = getSharedPreferences("city_state",
	// MODE_PRIVATE);
	// sh.edit().putInt("city", -1).commit();
	//
	// finish();
	// //initViews();
	// }
	// };

	private void initViews() {

		ArrayList<String> all = new ArrayList<String>();

		all.add("现货");
		all.add("期货");
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_single_choice,
				android.R.id.text1, all) {
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				View v = super.getView(position, convertView, parent);

				TextView tv = (TextView) v.findViewById(android.R.id.text1);
				tv.setTextSize(18);
				v.setMinimumHeight(150);

				return v;
			}
		};
		list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

		list.setAdapter(adapter);
		list.setItemChecked(mApp.getmCarStateValue(), true);
		adapter.notifyDataSetChanged();

	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View view,
			int position, long id) {

		if (updateState) {
			mApp.setEditor(true);
		}
		mApp.setmCarStateValue(position);
		if (position == 0) {
			finish();
		} else if (position == 1) {

			createDateDialog();
		}

	}

	private void createDateDialog() {

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("设定期货日期");
		builder.setIcon(android.R.drawable.ic_lock_idle_alarm);
		View v = LayoutInflater.from(this).inflate(R.layout.selector_qx_date,
				null);
		builder.setView(v);

		final DatePicker d = (DatePicker) v.findViewById(R.id.datePicker);
		// 设置确定和取消按钮
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				int year = d.getYear();
				int month = d.getMonth();
				int day = d.getDayOfMonth();
				String dateValue = year + "-" + (month + 1) + "-" + day;
				mApp.setmQHDate(dateValue);
				// System.out.println();

				finish();
			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub

			}
		});
		builder.create().show();

	}
}
